/**   
* @Title: SystemController.java 
* @Package com.zjdiepu.www.manage.api.zpp.controller 
* 
* @author zhangpp
* @date 2017年6月21日 下午8:52:22 
* @version V1.0   
*/
package com.zjdiepu.www.manage.api.zpp.controller;
import io.swagger.annotations.Api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.BaseDistinguishMapper;
import com.zjdiepu.www.dao.other.OtherInboxMapper;
import com.zjdiepu.www.dao.other.OtherMessagePushExtraMapper;
import com.zjdiepu.www.ibiz.system.IBaseSysParamsService;
import com.zjdiepu.www.ibiz.zpp.PublicService;
import com.zjdiepu.www.ibiz.zpp.SystemService;
import com.zjdiepu.www.model.base.dto.BaseDistinguish;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.other.dto.OtherInbox;
import com.zjdiepu.www.model.other.dto.OtherMessagePushExtra;
import com.zjdiepu.www.model.other.ext.OtherInboxExt;
import com.zjdiepu.www.model.system.dto.BaseSysParams;
import com.zjdiepu.www.model.system.ext.BaseSysParamsExt;
import com.zjdiepu.www.util.PasswordUtil;

/**      
 * 项目名称：diepu-parent-manage   
 * 类名称：SystemController   
 * 类描述：   
 * 创建人：zhangpp
 * 创建时间：2017年6月21日 下午8:52:22   
 * 修改人：zhangpp
 * 修改时间：2017年6月21日 下午8:52:22   
 * 修改备注：   
 * @version V1.0     
 */
@RestController
@RequestMapping(value= "SystemController", produces="application/json;charset=UTF-8")
@Api(tags = {"系统管理"}, produces="application/json;charset=UTF-8")
public class SystemController extends BaseController{

	private  final Logger logger = LoggerFactory.getLogger(SystemController.class);
	
	@Resource
    private IBaseSysParamsService baseSysParamsService;
	
	@Resource
	private PublicService publicService;
	
	@Resource
	private SystemService systemService;
	
	@Resource
	private OtherInboxMapper otherInboxMapper;
	
	@Resource
	private BaseDistinguishMapper baseDistinguishMapper;
	
	@Resource
	private OtherMessagePushExtraMapper otherMessagePushExtraMapper;
	
	//系统设置>额外信息推送配置
	@RequestMapping(value = "otherMessagePushExtraMapperUpdate", method = RequestMethod.POST)
    public Map<String, Object> otherMessagePushExtraMapperUpdate(OtherMessagePushExtra otherMessagePushExtra)
    {
    	Map<String, Object> maps = success();
    	if(null == otherMessagePushExtra)
    	{
    		throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
    	}
    	int count = otherMessagePushExtraMapper.updateByPrimaryKeySelective(otherMessagePushExtra);
    	if(count < 1)
    	{
    		throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"otherMessagePushExtraMapper updateByPrimaryKeySelective error");
    	}
		return maps;
    }
	
	//系统设置>额外信息推送配置
	@RequestMapping(value = "otherMessagePushExtraMapperDel", method = RequestMethod.POST)
    public Map<String, Object> otherMessagePushExtraMapperDel(String ids)
    {
    	Map<String, Object> maps = success();
    	if(StringUtils.isBlank(ids))
    	{
    		throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
    	}
    	String[] T = ids.split(",");
    	if(null != T && T.length > 0)
    	{
    		for(int i=0;i< T.length;i++)
        	{
    			OtherMessagePushExtra otherMessagePushExtra = otherMessagePushExtraMapper.selectByPrimaryKey(T[i]);
    			if(null != otherMessagePushExtra)
    			{
    				otherMessagePushExtra.setDpStatus(3);
    				int count = otherMessagePushExtraMapper.updateByPrimaryKeySelective(otherMessagePushExtra);
    				if(count < 1)
    		    	{
    		    		throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"otherMessagePushExtraMapper updateByPrimaryKeySelective error");
    		    	}
    			}
        	}
    	}
		return maps;
    }
	
	//系统设置>额外信息推送配置
	@RequestMapping(value = "otherMessagePushExtraMapperInsert", method = RequestMethod.POST)
    public Map<String, Object> otherMessagePushExtraMapperInsert(OtherMessagePushExtra otherMessagePushExtra)
    {
    	Map<String, Object> maps = success();
    	SysUser currentUser = (SysUser) getCurrentUser();
    	if(null == otherMessagePushExtra)
    	{
    		throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
    	}
    	
    	if(StringUtils.isNotBlank(otherMessagePushExtra.getIdUnit()))
    	{
    		String[] idUnitT = otherMessagePushExtra.getIdUnit().split(",");
    		String[] codeUnitT = otherMessagePushExtra.getCodeUnit().split(",");
    		if(null != idUnitT && idUnitT.length > 0)
    		{
    			for(int i=0;i<idUnitT.length;i++)
    			{
    				OtherMessagePushExtra pushExtra = new OtherMessagePushExtra();
    				pushExtra.initInsertData(currentUser.getId(), currentUser.getName());
    				pushExtra.setCodeUnit(codeUnitT[i]);
    				pushExtra.setIdUnit(idUnitT[i]);
    				pushExtra.setUserId(otherMessagePushExtra.getUserId());
    				pushExtra.setTypeMsgEvent(otherMessagePushExtra.getTypeMsgEvent());
    				pushExtra.setStatusMsgHandle(otherMessagePushExtra.getStatusMsgHandle());
    				pushExtra.setValueMsgHandle(otherMessagePushExtra.getValueMsgHandle());
        	    	int count = otherMessagePushExtraMapper.insert(pushExtra);
        	    	if(count < 1)
        	    	{
        	    		throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"otherMessagePushExtraMapper insert error");
        	    	}
    			}
    		}
    	}
		return maps;
    }
	
	
	/**
	 * 查询全部消息
	 * @param otherInboxExt
	 * @return
	 */
	@RequestMapping(value = "getOtherInboxMapperList", method = RequestMethod.POST)
	public ApiResponse getOtherInboxMapperList(OtherInboxExt otherInboxExt)
	{
		 if(null == otherInboxExt)
		 {
			 throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
		 }
		 Map<String, Object> maps = success();
		 otherInboxExt.notDeleted();
		 SysUser currentUser = (SysUser) getCurrentUser();
		 
		 if(null == currentUser)
		 {
			 throw new BaseException(ResultFlagType.LOGIN_TIMEOUT.getCode().toString(),ResultFlagType.LOGIN_TIMEOUT.getValue());
		 }
		 
		 otherInboxExt.setReceiverId(currentUser.getId());
		 otherInboxExt.setOrderSql(" created desc ");
		 otherInboxExt.setRows(20);
		 List<OtherInbox> result = otherInboxMapper.selectByExt(otherInboxExt);
		 if(null != result && result.size() > 0)
		 {
			 for(int i=0;i<result.size();i++)
			 {
				 OtherInbox otherInbox = result.get(i);
				 if(null != otherInbox)
				 {
					 otherInbox.setContentInbox(otherInbox.getContentInbox().replace("/n", "</br>"));
				 }
			 }
		 }
		 maps.put("result", result);
		 String sqlXmls = "select count(t.id) count from other_inbox t where t.receiver_id = '"+currentUser.getId()+"' and t.status_inbox = 1 and t.dp_status = 1";
		 int count = publicService.countSqlXmls(sqlXmls);
		 maps.put("count", count);
		 return ApiResponse.success(maps);
	}
	
	/**
	 * 查询消息
	 * @return
	 */
	@RequestMapping(value = "getOtherInboxMapper", method = RequestMethod.POST)
    public ApiResponse getOtherInboxMapper()
	{
		OtherInbox otherInbox = new OtherInbox();
		SysUser currentUser = (SysUser) getCurrentUser();
		otherInbox.setReceiverId(currentUser.getId());
		otherInbox.setStatusInbox(1);
		otherInbox.setDpStatus(1);
		otherInbox = otherInboxMapper.selectRefined(otherInbox);
		if(null != otherInbox)
		{
			otherInbox.setContentInbox(otherInbox.getContentInbox().replace("/n", "</br>"));
		}
        return ApiResponse.success(otherInbox);
    }
	
	/**
	 * 修改提示消息
	 * @return
	 */
	@RequestMapping(value = "upOtherInbox", method = RequestMethod.POST)
    public ApiResponse upOtherInbox(String replaceId)
	{
		OtherInbox otherInbox = new OtherInbox();
		otherInbox.setId(replaceId);
		otherInbox.setStatusInbox(2);
		otherInboxMapper.updateByPrimaryKeySelective(otherInbox);
        return ApiResponse.success(true);
    }
	
	/**
	* 验证编码是否重复
	* @author zhangpp
	* @Title: getSystemParamsCount 
	* 
	* @param @param baseSysParamsExt
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws
	 */
    @RequestMapping(value = "getSystemParamsCount", method = RequestMethod.POST)
    public ApiResponse getSystemParamsCount(@ModelAttribute BaseSysParamsExt baseSysParamsExt){
		BaseSysParams baseSysParams = new BaseSysParams();
		baseSysParams.setCodeParam(baseSysParamsExt.getCodeParam());
		baseSysParams = baseSysParamsService.selectRefined(baseSysParams);
        return ApiResponse.success(baseSysParams);
    }
	
	/**
	*  查询客户结清数量
	*  @author  zhangpp
	*  @Title:  getSettleCount  
	*    
	*  @param  @param busiId
	*  @param  @return        设定文件  
	*  @return  Map<String,Object>        返回类型  
	*  @version  V1.0
	*  @throws
	 */
	@RequestMapping(value = "getSettleCount", method = RequestMethod.POST)
    public Map<String, Object> getSettleCount(String busiId)
    {
    	Map<String, Object> maps = success();
    	String sqlXmls = "select count(t.id) count from dh_settle t where t.busi_id = '"+busiId+"' and t.dp_status = 1";
    	int count = publicService.countSqlXmls(sqlXmls);
    	maps.put("count", count);
		return maps;
    }
	
	/**
	*  修改秘密
	*  @author  zhangpp
	*  @Title:  passNew  
	*    
	*  @param  @param pass
	*  @param  @param passNew
	*  @param  @param passNew1
	*  @param  @return        设定文件  
	*  @return  Map<String,Object>        返回类型  
	*  @version  V1.0
	*  @throws
	 */
	@RequestMapping(value = "passNew", method = RequestMethod.POST)
	public Map<String, Object> passNew(String pass,String passNew, String passNew1,HttpServletRequest request)
	{
		Map<String, Object> maps = success();
		SysUser currentUser = (SysUser) getCurrentUser();
		String loginName = currentUser.getLoginName();
		pass = PasswordUtil.getDbPasswordForMd5(loginName, pass);
		passNew = PasswordUtil.getDbPasswordForMd5(loginName, passNew);
		passNew1 = PasswordUtil.getDbPasswordForMd5(loginName, passNew1);
		currentUser.initUpdateData(currentUser.getId(), currentUser.getName());
		systemService.passNew(currentUser, pass, passNew, passNew1);
		removeCurrentUser();
		return maps;
	}
	
	//操作系统分辨率
	@RequestMapping(value = "diepubaseDistinguish", method = RequestMethod.POST)
	public Map<String, Object> diepubaseDistinguish(BaseDistinguish baseDistinguish)
	{
		Map<String, Object> maps = success();
		try 
		{
			BaseDistinguish distinguish = baseDistinguishMapper.selectRefined(baseDistinguish);
			if(null == distinguish)
			{
				AbstractSysUser currentUser = getCurrentUser();
				baseDistinguish.initInsertData(currentUser.getId(), currentUser.getName());
				baseDistinguishMapper.insert(baseDistinguish);
			}
		} 
		catch (Exception e) 
		{
			
			logger.error(e.getMessage());
		}
		return maps;
	}
}
